from uuid import uuid1
if __name__ == '__main__':
    from conn_mysql import ConnectMysql
else:
    from .conn_mysql import ConnectMysql
import pymysql

class MysqlApi(ConnectMysql):

    def __init__(self):

        super().__init__()

    def select_for_company_id(self, ID):
        '''传入company_id获取company表中对应的信息'''

        sql = "SELECT * FROM company WHERE id=%s"

        return self.select_one(sql, (ID,))
    
    def update_need(self, company_id, need):
        '''用户发布需求'''
        sql = "UPDATE company SET need=%s"

        return self.update_info(sql, (need, company_id))

    def select_user(self, phone, pass_word):#传入用户名和密码找到对应的数据
        '''#传入用户名和密码找到company表中对应的数据'''

        sql = 'SELECT * FROM company WHERE phone=%s and pass_word=%s'

        return self.select_one(sql, (phone, pass_word))

    def select_openid(self, openid):#传入token返回token表对应的数据

        '''传入openid返回openid表对应的数据'''

        sql = "SELECT * FROM openid WHERE openid=%s"

        return self.select_one(sql, (openid))

    def insert_token(self, token, company_id):#传入公司ID,在token表中增加一个token值
        '''传入公司ID,在token表中增加一个token值
        company_id: int #公司的id
        return: 0  #回定的
        '''

        sql = 'INSERT INTO token(token, company_id) VALUES (%s, %s)'

        return self.insert_info(sql, (token, company_id))

    def insert_register(self, info_dict):
        '''向company表中插入注册数据
        '''

        args = list(info_dict.values())


        if not all(args):

            return {

                "status": -1,
                "errorCode": 21,
                "errorMsg": "确少参数",
                "resultBody": None          
            }

        sql = '''INSERT INTO company
        (company_name, product_name, need, linkman, phone, party, pass_word, openid)
        values
        (%s,%s,%s,%s,%s,%s,%s,%s);       
        '''
        try:
            self.insert_info(sql, args)

        except pymysql.err.IntegrityError as e:

            if e.args[0] == 1062:

                return {
                    "status": -1,
                    "errorCode": e.args[0],
                    "errorMsg": e.args[1],
                    "resultBody": None          
                }
        else:

            return {

                    "status": 1,
                    "errorCode": None,
                    "errorMsg": None,
                    "resultBody": "注册成功"          
                }





    
